package com.hbnu.cmd.view;

import com.hbnu.cmd.controller.MainController;
import com.hbnu.cmd.util.Input;
import com.hbnu.common.entity.User;

/**
 * 主菜单
 */

public class MainView {
    private static final Input input = Input.getInstance();
    private final User loginUser;
    private final MainController mainController;

    /**
     * 构造方法
     *
     * @param loginUser 登录用户
     */
    public MainView(User loginUser) {
        this.loginUser = loginUser;
        this.mainController = new MainController(loginUser);
    }

    /**
     * 显示主菜单
     */
    public void show() {
        System.out.println("\n===== 打卡记录系统 =====");
        while (true) {
            System.out.println("\n请选择操作：");
            System.out.println("1. 修改密码");
            System.out.println("2. 打卡项管理");
            System.out.println("3. 退出登录");
            System.out.print("输入数字(1-3)：");

            Integer choice = input.readInt();
            if (choice == null) {
                System.err.println("输入异常，请重试！");
                continue;
            }

            switch (choice) {
                case 1:
                    // 密码修改成功则退回到登录界面重新登录
                    if (handleUpdatePassword()) {
                        return;
                    }
                    break;
                case 2:
                    showCheckInItemMenu();
                    break;
                case 3:
                    System.out.println("已退出登录！");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }

    /**
     * 处理密码修改
     *
     * @return 密码修改成功则返回true，否则返回false
     */
    private boolean handleUpdatePassword() {
        System.out.println("请输入原密码");
        String oldPassword = input.readLineTrim();
        if (!oldPassword.equals(loginUser.getPassword())) {
            System.err.println("原密码错误！");
            return false;
        }

        System.out.println("请输入新密码");
        String newPassword = input.readLineTrim();

        System.out.println("请再次输入新密码");
        String confirmPassword = input.readLineTrim();

        mainController.handleUpdatePassword(newPassword, confirmPassword);
        return true;
    }

    /**
     * 打卡项管理菜单
     */
    private void showCheckInItemMenu() {
        while (true) {
            System.out.println("\n===== 打卡项管理 =====");
            System.out.println("1. 新建打卡项");
            System.out.println("2. 查看我的打卡项");
            System.out.println("3. 今日打卡");
            System.out.println("4. 修改打卡项");
            System.out.println("5. 删除打卡项");
            System.out.println("6. 返回主菜单");
            System.out.print("请选择（1-6）：");

            Integer choice = input.readInt();
            if (choice == null) {
                System.err.println("输入异常，请重试！");
                continue;
            }

            switch (choice) {
                case 1:
                    handleAddCheckInItem();
                    break;
                case 2:
                    handleGetAllCheckInItems();
                    break;
                case 3:
                    handleCheckIn();
                    break;
                case 4:
                    handleUpdateCheckInItem();
                    break;
                case 5:
                    handleDeleteCheckInItem();
                    break;
                case 6:
                    return;
                default:
                    System.err.println("输入错误，请重新选择！");
            }
        }
    }

    /**
     * 处理新建打卡项
     */
    private void handleAddCheckInItem() {
        System.out.println("\n===== 新建打卡项 =====");

        System.out.print("请输入打卡项名称：");
        String itemName = input.readLineTrim();
        if (itemName.isEmpty()) {
            System.err.println("打卡项名称不能为空！");
            return;
        }

        mainController.handleAddCheckInItem(itemName);
    }

    /**
     * 处理查看我的打卡项
     */
    private void handleGetAllCheckInItems() {
        System.out.print("\n" + "=".repeat(28));
        System.out.print(" 所有打卡项 ");
        System.out.println("=".repeat(28));

        System.out.printf("%-5s%-20s%-10s%-12s%-12s%n",
                "ID", "打卡项名称", "今日状态", "总打卡天数", "连续打卡天数");

        System.out.println("-".repeat(66));
        mainController.handleGetAllCheckInItems();

        System.out.println("\n已显示所有打卡项！");
    }

    /**
     * 处理今日打卡
     */
    private void handleCheckIn() {
        System.out.println("\n===== 今日打卡 =====");

        System.out.print("请输入打卡项ID: ");
        int itemId = input.readInt();

        mainController.handleCheckIn(itemId);
    }

    /**
     * 处理修改打卡项
     */
    private void handleUpdateCheckInItem() {
        System.out.println("\n===== 修改打卡项 =====");

        System.out.print("请输入打卡项ID: ");
        int itemId = input.readInt();

        System.out.print("请输入新的打卡项名称：");
        String newName = input.readLineTrim();

        mainController.handleUpdateCheckInItem(itemId, newName);
    }

    /**
     * 处理删除打卡项
     */
    private void handleDeleteCheckInItem() {
        System.out.println("\n===== 删除打卡项 =====");

        System.out.print("请输入打卡项ID: ");
        int itemId = input.readInt();

        System.out.println("确定要删除该打卡项吗？（输入y确认）");
        String choice = input.readLineTrim();

        if (!choice.equalsIgnoreCase("y")) {
            System.out.println("已取消删除！");
            return;
        }

        mainController.handleDeleteCheckInItem(itemId);
    }
}
